package com.qiniuyun.backai.controller;

import com.qiniuyun.backai.common.PageResult;
import com.qiniuyun.backai.common.R;
import com.qiniuyun.backai.domain.vo.CharacterVO;
import com.qiniuyun.backai.service.ICharacterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 角色控制器
 * @author 王程楠
 * @since 2025-09-23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/characters")
@Tag(name = "角色管理", description = "角色相关接口")
@Validated
public class CharacterController {

    private final ICharacterService characterService;

    @GetMapping
    @Operation(summary = "角色分页查询", description = "分页查询角色列表")
    public R<PageResult<CharacterVO>> pageCharacters(
            @Parameter(description = "页码，从0开始", example = "0")
            @RequestParam
            @NotNull(message = "页码不能为空")
            @Min(value = 0, message = "页码不能小于0")
            Integer page,

            @Parameter(description = "每页记录数", example = "10")
            @RequestParam
            @NotNull(message = "每页记录数不能为空")
            @Min(value = 1, message = "每页记录数不能小于1")
            Integer size) {

        PageResult<CharacterVO> result = characterService.pageCharacters(page, size);
        return R.ok(result);
    }

    @GetMapping("/{charId}")
    @Operation(summary = "角色详情查询", description = "根据ID查询角色详情")
    public R<CharacterVO> getCharacterById(
            @Parameter(description = "角色ID", example = "1")
            @PathVariable Long charId) {

        CharacterVO character = characterService.getCharacterById(charId);
        if (character == null) {
            return R.fail(404, "角色不存在");
        }
        return R.ok(character);
    }

    @GetMapping("/search")
    @Operation(summary = "角色搜索", description = "根据关键词搜索角色")
    public R<List<CharacterVO>> searchCharacters(
            @Parameter(description = "搜索关键词", example = "苏格拉底")
            @RequestParam String keyword) {

        List<CharacterVO> characters = characterService.searchCharacters(keyword);
        return R.ok(characters);
    }
}
